package net.shopxx.action.admin;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.Pointproduct;
import net.shopxx.service.PointproductService;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 积分商品
 */

@ParentPackage("admin")
public class PointproductAction extends BaseAdminAction {

	private static final long serialVersionUID = -4433964283757192334L;

	@Resource
	private PointproductService pointproductService;
	
	private Pointproduct pointproduct;
	
	private File imageUrl;
    private String imageUrlFileName;
	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
	    pointproduct = pointproductService.load(id);
		return INPUT;
	}

	// 列表
	public String list() {
		pager = pointproductService.findByPager(pager); 
		return LIST;
	}

	// 删除
	public String delete() throws Exception {
		pointproductService.delete(ids);
		flushCache();
		return ajaxJsonSuccessMessage("删除成功！");
	}

	// 保存
	@Validations(
		requiredStrings = {
			@RequiredStringValidator(fieldName = "pointproduct.name", message = "商品名称不允许为空!") 
		}, 
		requiredFields = {
		    @RequiredFieldValidator(fieldName = "pointproduct.enable", message = "是否可用!"),              
		},
		intRangeFields = {
			@IntRangeFieldValidator(fieldName = "pointproduct.point", min = "0", message = "积分必须为零或正整数!"),
		}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
	    if (imageUrl != null){
            String savePath = validateFile(imageUrl,imageUrlFileName);
            if(ERROR.equals(savePath))
            {
                return ERROR;
            }
             
            pointproduct.setImageUrl(savePath);
        }
	    pointproductService.save(pointproduct);
		redirectionUrl = "pointproduct!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(
        requiredStrings = {
            @RequiredStringValidator(fieldName = "pointproduct.name", message = "商品名称不允许为空!") 
        }, 
        requiredFields = {
            @RequiredFieldValidator(fieldName = "pointproduct.enable", message = "是否可用!"),              
        },
        intRangeFields = {
            @IntRangeFieldValidator(fieldName = "pointproduct.point", min = "0", message = "积分必须为零或正整数!"),
        }
    )
	@InputConfig(resultName = "error")
	public String update() throws Exception {
	    if (imageUrl != null){
            String savePath = validateFile(imageUrl,imageUrlFileName);
            if(ERROR.equals(savePath))
            {
                return ERROR;
            }
             
            pointproduct.setImageUrl(savePath);
        }
	    Pointproduct persistent = pointproductService.load(id); 
        BeanUtils.copyProperties(pointproduct, persistent, new String[] {"id", "createDate", "modifyDate"});
        pointproductService.update(persistent);
		redirectionUrl = "pointproduct!list.action"; 
		return SUCCESS;
	}
	

	
	
	public Pointproduct getPointproduct() {
        return pointproduct;
    }

    public void setPointproduct(Pointproduct pointproduct) {
        this.pointproduct = pointproduct;
    }

    // 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}

    public File getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(File imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getImageUrlFileName() {
        return imageUrlFileName;
    }

    public void setImageUrlFileName(String imageUrlFileName) {
        this.imageUrlFileName = imageUrlFileName;
    }
	
	

}